package com.zwr.sheller.customer.Service.Impl;

import com.zwr.sheller.core.common.WDMBusinessException;
import com.zwr.sheller.core.common.WDMExceptionCode;
import com.zwr.sheller.core.pojo.Address;
import com.zwr.sheller.core.service.impl.BaseServiceImpl;
import com.zwr.sheller.customer.Service.AddressService;
import com.zwr.sheller.customer.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 *
 *@author Kang
 *@date
 */
@Service("/addressService")
public class AddressServiceImpl extends BaseServiceImpl<Address> implements AddressService {

    @Autowired(required = false)
    AddressMapper addressMapper;
    @Override
    public Integer add(Address address) {

        int insert = addressMapper.insert(address);
        return insert;
    }

    @Override
    public Integer delete(Long id) {
        Address address=new Address();
        address.setAddressId(id);
        int delete = addressMapper.delete(address);
        return delete;
    }

    @Override
    public Map<String, Object> selectAll(Long id) {
        Map<String, Object> map = addressMapper.selectById(id);
        return map;
    }

    @Override
    public Integer update(Address address) {

        int i = addressMapper.updateByPrimaryKeySelective(address);
        return i;
    }

    @Override
    public List<Address> selectAddressByUserId(Long userId) throws WDMBusinessException {
        Example example = new Example(Address.class);
        example.createCriteria().andEqualTo("userId",userId);
        List<Address> addresses = addressMapper.selectByExample(example);
        if (addresses.size()<0){
            throw  new WDMBusinessException(WDMExceptionCode.SELECT_FAILED);
        }
        return addresses;
    }


}
